//
//  DataBase.m
//  XhlTrip
//
//  Created by dllo on 15/8/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DataBase.h"
#import "Db.h"
#import "DBModel.h"


@implementation DataBase
// 添加方法
+ (void)insertWithModel:(DBModel *)model
{
    // 第一步 获得数据库指针
    sqlite3 *db = [Db dabopen];
    
    // 第二步 获得数据库语句
    NSString *str = [NSString stringWithFormat:@"insert into newtable(id, photos_count, start_date, name, days, front_cover_photo_url) values('%@', '%@', '%@', '%@', '%@' ,'%@')", model.number    ,model.photos_count, model.start_date, model.name, model.days, model.front_cover_photo_url];
    
    // 第三步 执行语句
    int result = sqlite3_exec(db, [str UTF8String],nil, nil, nil);
    
    // 判断执行是否为空
    if (result == SQLITE_OK) {
        NSLog(@"插入OK");
    }else
    {
        NSLog(@"插入失败");
    }
}


+ (void)deleteWithModel:(DBModel *)model
{
    // 第一步 获得数据库指针
    sqlite3 *db = [Db dabopen];
    // 第二步 获得数据库语句
    NSString *str = [NSString stringWithFormat:@"delete from newtable where id='%@'", model.number];
    // 第三步 执行语句
    int result = sqlite3_exec(db, [str UTF8String],nil, nil, nil);
    
    
    // 判断执行是否为空
    if (result == SQLITE_OK) {
        NSLog(@"删除OK");
    }else
    {
        NSLog(@"执行失败");
    }
}


// 查询所有的
+ (NSArray *)selectall
{
    
    // 第一步 获得数据库指针
    sqlite3 *db = [Db dabopen];
    
    // 第二步 定义数据库替身
    
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select * from newtable"];    // 第三步 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *aid = sqlite3_column_text(stmt, 0);
            const unsigned char *aphotoCount = sqlite3_column_text(stmt, 1);
            const unsigned char *astartdate = sqlite3_column_text(stmt, 2);
            const unsigned char *aname = sqlite3_column_text(stmt, 3);
            const unsigned char *adays = sqlite3_column_text(stmt, 4);
            const unsigned char *frontphotourl = sqlite3_column_text(stmt, 5);
            
            NSString *a_id = [NSString stringWithUTF8String:(const char *)aid];
            NSNumber *number = [NSNumber numberWithInteger:[a_id integerValue]];
            
            NSString *name = [NSString stringWithUTF8String:(const char *)aname];
            
            NSString *pcount = [NSString stringWithUTF8String:(const char *)aphotoCount];
            NSNumber *photo_count = [NSNumber numberWithInteger:[pcount integerValue]];
            
            NSString *startdate = [NSString stringWithUTF8String:(const char *)astartdate];
            
            NSString *days = [NSString stringWithUTF8String:(const char *)adays];
            NSNumber *day = [NSNumber numberWithInteger:[days integerValue]];
            
            
            NSString *photo = [NSString stringWithUTF8String:(const char *)frontphotourl];
            NSURL *photo_url = [NSURL URLWithString:photo];
            
    
            DBModel *model = [DBModel dbModelWith:name photos_count:photo_count start_date:startdate days:day front_cover_photo_url:photo_url number:number];
            [array addObject:model];
        }
        
    }
    sqlite3_finalize(stmt);
    return array;
    
    
}


+ (DBModel *)selectoneWith:(NSString *)newsId
{
    
    
    // 第一步 获得数据库指针
    sqlite3 *db = [Db dabopen];
    
    // 第二步 定义数据库替身
    
    sqlite3_stmt *stmt = nil;
    NSString *str = [NSString stringWithFormat:@"select * from newtable where id='%@'", newsId];   // 第三步 执行
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        // 判断是否有下一行数据可用于读取
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *aid = sqlite3_column_text(stmt, 0);
            const unsigned char *aphotoCount = sqlite3_column_text(stmt, 1);
            const unsigned char *astartdate = sqlite3_column_text(stmt, 2);
            const unsigned char *aname = sqlite3_column_text(stmt, 3);
            const unsigned char *adays = sqlite3_column_text(stmt, 4);
            const unsigned char *frontphotourl = sqlite3_column_text(stmt, 5);
            
            NSString *a_id = [NSString stringWithUTF8String:(const char *)aid];
            NSNumber *number = [NSNumber numberWithInteger:[a_id integerValue]];
            
            NSString *name = [NSString stringWithUTF8String:(const char *)aname];
            
            NSString *pcount = [NSString stringWithUTF8String:(const char *)aphotoCount];
            NSNumber *photo_count = [NSNumber numberWithInteger:[pcount integerValue]];
            
            NSString *startdate = [NSString stringWithUTF8String:(const char *)astartdate];
        
            NSString *days = [NSString stringWithUTF8String:(const char *)adays];
            NSNumber *day = [NSNumber numberWithInteger:[days integerValue]];
            
            
            NSString *photo = [NSString stringWithUTF8String:(const char *)frontphotourl];
            NSURL *photo_url = [NSURL URLWithString:photo];

      
            
            DBModel *model = [DBModel dbModelWith:name photos_count:photo_count start_date:startdate days:day front_cover_photo_url:photo_url number:number];
            [array addObject:model];
            return model;
            
        }
        
    }
    sqlite3_finalize(stmt);
    
    return nil;
}

@end
